﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;

using MVCnExt.Web.UI.Ext;
using System.Text;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// A specialized contentContainer representing the viewable application area (the browser viewport).
    /// </summary>
    [Description("A specialized contentContainer representing the viewable application area (the browser viewport).")]
    public class ExtViewport : ExtComponent, INamingContainer
    {
        private List<ExtComponent> _items = new List<ExtComponent>();
        private List<ScriptReference> _references = new List<ScriptReference>();

        public override List<ScriptReference> GetScriptReferences()
        {
            _references.AddRange(base.GetScriptReferences());

            return _references;
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.Viewport";

            String[] properties = new String[]{                
                 "layout"
            };

            Type component = typeof(ExtViewport);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtViewport.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                    continue;

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            //add items
            if (items.Count != 0)
            {
                // build items list
                StringBuilder itemList = new StringBuilder("[");

                for (int i = 0; i < items.Count; i++)
                {
                    // add comma between items
                    if (i != 0)
                    {
                        itemList.Append(",");
#if DEBUG
                        itemList.AppendLine("\t\t\t");
#endif
                    }

                    // get the item script
                    IList<ScriptDescriptor> descriptors = items[i].GetScriptDescriptors();

                    // get the items's list of script dependencies
                    List<ScriptReference> references = items[i].GetScriptReferences();

                    //make sure the script dependencies are registered
                    _references.AddRange(references);

                    // write the item's config to the list
                    itemList.Append(((ExtScriptControlDescriptor)descriptors[0]).WriteScript(ScriptType.NewOnly));
                }

                itemList.Append("]");

                desc.AddPropertyBase("items", itemList);
            }

            // applyTo
            if (!String.IsNullOrEmpty(applyTo))
            {
                if (String.Compare(applyTo, ID, true) == 0)
                    desc.AddPropertyBase("applyTo", String.Format("'{0}'", ClientID));
                else
                    desc.AddPropertyBase("applyTo", String.Format("'{0}'", GetControlID(applyTo)));
            }
            // contentEl
            if (!String.IsNullOrEmpty(contentEl))
            {
                if (String.Compare(contentEl, ID, true) == 0)
                    desc.AddPropertyBase("contentEl", String.Format("'{0}'", ClientID));
                else
                    desc.AddPropertyBase("contentEl", String.Format("'{0}'", GetControlID(contentEl)));
            }            

            return list;
        }
        

        /// <summary>
        /// Instantiate the Body template so we can add child controls
        /// </summary>
        protected override void CreateChildControls()
        {
            //call base method
            base.CreateChildControls();
            Type[] genericArguments = new Type[] { items.GetType() };

            base.InstantiateTemplate<ExtComponent>(items);
        }

       
        #region Public Properties

        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public virtual List<ExtComponent> items
        {
            get
            {
                return _items;
            }
        }

        /// <summary>
        /// The id (ClientId) of the node, a DOM node or an existing Element corresponding to a DIV
        /// </summary>
        public String applyTo { get; set; }

        public String contentEl { get; set; }

        public String layout { get; set; }

        #endregion
    }
}
